var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var check = require('../../../utils/check.js');

var app = getApp();
Page({
  data: {
    //发布地址
    addressid: '',
    name: '',
    mobile: '',
    region: ["省", "市", "区"],
    address: '',
    isDefault: 0
  },
  bindinputMobile(event) {
    var mobile = event.detail.value;
    this.setData({
      mobile: mobile
    });
  },
  bindinputName(event) {
    var name = event.detail.value;
    this.setData({
      name: name
    });
  },
  bindinputAddress(event) {
    var address = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault(event) {
    if (this.data.isDefault == 0) {
      this.setData({
        isDefault: 1
      });
    } else {
      this.setData({
        isDefault: 0
      });
    }

  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let that = this;
    if (options.id) {
      that.setData({
        addressid: options.id
      })
    }
    that.getAddressDetail(options.id);
  },
  onReady: function () {

  },
  getAddressDetail() {
    let that = this;
    util.request(api.MemberAddressDetail, {
      id: that.data.addressid
    }).then(function (res) {
      if (res.result == 1) {
        var region = [];
        region.push(res.data.province);
        region.push(res.data.area);
        region.push(res.data.city);
        that.setData({
          name: res.data.name,
          mobile: res.data.mobile,
          region: region,
          address: res.data.detailaddress,
          isDefault: res.data.isDefault,
        });
      }
    });
  },
  saveAddress() {
    if (this.data.name == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (this.data.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }


    if (this.data.address == '') {
      util.showErrorToast('请输入省市区');
      return false;
    }

    if (this.data.address == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }

    if (!check.isValidPhone(this.data.mobile)) {
      util.showErrorToast('手机号不正确');
      return false;
    }

    let that = this;
    util.request(api.MemberAddressSave, {
      id: that.data.addressid,
      name: that.data.name,
      mobile: that.data.mobile,
      province: that.data.region[0],
      city: that.data.region[1],
      area: that.data.region[2],
      address: that.data.address,
      isDefault: that.data.isDefault
    }, 'POST').then(function (res) {
      if (res.result == 1) {
        //返回之前，先取出上一页对象，并设置addressId
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        console.log(prevPage);
        if (prevPage.route == "pages/order/order") {
          prevPage.setData({
            addressId: res.data.addressid
          })
          try {
            wx.setStorageSync('addressid', res.data.addressid);
          } catch (e) {}
          console.log("set address");
        }
        wx.navigateBack();
      }
    });
  },

  //取消返回上一页
  cancelAddress: function (e) {
    wx.navigateBack();
  },

  //发布地址:事件
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    });
  },

  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})